<?php
# MySQL Library
#
# By 1337D00D
# 
# This is a library for handling SQL connections.
# You do not need to edit anything in here.

class SQL
{
	public $Connection;
	public $Result;
	public $Error;
	public $Type;
	
	public function Query($string,$resultmode=0)
	{
		switch($this->Type)
		{
		case "mysql":
			$result = @mysql_query($string,$this->Connection);
			switch($resultmode)
			{
			default:
				return $this->Result = $result;
				break;
			case 1:
				return $this->Result = @mysql_fetch_array($result);
				break;
			case 2:
				return $this->Result = @mysql_num_rows($result);
				break;
			case 3:
				$i=0;
				while($row = @mysql_fetch_array($result))
				{
					$res[$i]=$row;
					$i++;
				}
				return $this->Result = $res;
				break;
			}
			break;
		case "postgresql":
			$result = @pg_query($this->Connection,$string);
			switch($resultmode)
			{
			default:
				return $this->Result = $result;
				break;
			case 1:
				return $this->Result = @pg_fetch_array($result);
				break;
			case 2:
				return $this->Result = @pg_num_rows($result);
				break;
			case 3:
				$i=0;
				while($row = @pg_fetch_array($result))
				{
					$res[$i]=$row;
					$i++;
				}
				return $this->Result = $res;
				break;
			}
			break;
		}
	}
	
	public function Sanitize($string)
	{
		return @mysql_real_escape_string($string);
	}
	
	public function __destruct()
	{
		switch($this->Type)
		{
		case "mysql":
			@mysql_close($this->Connection);
			break;
		case "postgresql":
			@pg_close($this->Connection);
			break;
		}
	}
	
	public function __construct($host = DBHOST, $user = DBUSER, $pass = DBPASS, $data = DBNAME, $type = SQLTYPE)
	{
		$this->Type = $type;
		switch($type)
		{
		case "mysql":
			if(!($con = @mysql_connect($host,$user,$pass)) || $con == NULL || !@mysql_select_db($data))
			{
				$this->Error = mysql_error();
				return false;
			}
			else
			{
				$this->Connection = $con;
			}
			break;
		case "postgresql":
			if(!($con = @pg_connect("host=$host user=$user dbname=$data password=$pass")))
			{
				$this->Error = pg_last_error();
				return false;
			}
			else
			{
				$this->Connection = $con;
			}
			break;
		}
	}
}


class MySQL
{
	public $Connection;
	public $Result;
	public $Error;
	
	public function Query($string,$resultmode=0)
	{
		$result = @mysql_query($string,$this->Connection);
		switch($resultmode)
		{
		default:
			return $this->Result = $result;
			break;
		case 1:
			return $this->Result = @mysql_fetch_array($result);
			break;
		case 2:
			return $this->Result = @mysql_num_rows($result);
			break;
		case 3:
			$i=0;
			while($row = @mysql_fetch_array($result))
			{
				$res[$i]=$row;
				$i++;
			}
			return $this->Result = $res;
			break;
		}
	}
	
	function __construct($host = DBHOST, $user = DBUSER, $pass = DBPASS, $data = DBNAME)
	{
		if(!($con = @mysql_connect($host,$user,$pass)) || $con == NULL || !@mysql_select_db($data))
		{
			$this->Error = @mysql_error();
			return false;
		}
		else
		{
			$this->Connection = $con;
		}
	}
}

class PostgreSQL
{
	public $Connection;
	public $Result;
	public $Error;
	
	public function Query($string,$resultmode=0)
	{
		$result = @pg_query($this->Connection,$string);
		switch($resultmode)
		{
		default:
			return $this->Result = $result;
			break;
		case 1:
			return $this->Result = @pg_fetch_array($result);
			break;
		case 2:
			return $this->Result = @pg_num_rows($result);
			break;
		case 3:
			$i=0;
			while($row = @pg_fetch_array($result))
			{
				$res[$i]=$row;
				$i++;
			}
			return $this->Result = $res;
			break;
		}
	}
	
	function __construct($host = DBHOST, $user = DBUSER, $pass = DBPASS, $data = DBNAME)
	{
		if(!($con = @pg_connect("host=$host user=$user dbname=$data password=$pass")))
		{
			$this->Error = @pg_last_error();
			return false;
		}
		else
		{
			$this->Connection = $con;
		}
	}
}

function NewSQL($host = DBHOST, $user = DBUSER, $pass = DBPASS, $data = DBNAME, $sqltype = SQLTYPE)
{
	switch($sqltype)
	{
	case "mysql":
		return new MySQL($host,$user,$pass,$data);
		break;
	case "postgresql":
		return new PostgreSQL($host,$user,$pass,$data);
		break;
	}
}	
?>